//
//  FaceView.m
//  sensen_ios
//
//  Created by pro on 16/3/2.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "FaceView.h"

// 表情
#define EMOJI_CODE_TO_SYMBOL(x) ((((0x808080F0 | (x & 0x3F000) >> 4) | (x & 0xFC0) << 10) | (x & 0x1C0000) << 18) | (x & 0x3F) << 24)

@interface FaceView () <UIScrollViewDelegate>
{
    UIScrollView *_scrollView; // 滑动视图
    UIPageControl *_pageControl; // 分页视图
    NSArray *arrEmotion;
}

@end

@implementation FaceView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.clipsToBounds = YES;  //超出边界裁剪
        self.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
        self.width = SCREEN_WIDTH;
        self.height = 216;
        [self _initViews];
        
    }
    
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {
    
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.frame = self.bounds;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    [self addSubview:_scrollView];
    
    // 分页视图
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    _pageControl.backgroundColor = [UIColor clearColor];
    _pageControl.currentPageIndicatorTintColor = ORANGE_COLOR;
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.bottom = self.height - 10;
    _pageControl.center = CGPointMake(self.width / 2, _pageControl.center.y);
    [self addSubview:_pageControl];
    
    //获取数组
    arrEmotion = [self defaultEmoticons];
    
    
    
    CGFloat w = 40;
    CGFloat h = 40;
    NSInteger oneRowNum = (NSInteger)(self.width / w); // 一行的个数
   
    NSInteger rows = (NSInteger)(_pageControl.top / h); // 行数
    NSInteger onePageNum = oneRowNum * rows; // 一页的表情数
    CGFloat remainW = (NSInteger)self.width % (NSInteger)w; // 余下的宽度
    CGFloat remainH = (NSInteger)_pageControl.top % (NSInteger)h; // 余下的高度

    NSInteger pages = arrEmotion.count / onePageNum + 1; // 根据表情总数和一页的数量计算页数
    // 给分页视图设置页数
    _pageControl.numberOfPages = pages;
    // 给滑动视图设置滑动范围
    _scrollView.contentSize = CGSizeMake(pages * _scrollView.width, 0);

    CGFloat marginW = remainW / 2;
    CGFloat marginH = remainH / 2;

    CGFloat x;
    CGFloat y;
    for (int i = 0; i < arrEmotion.count; i ++) {
        x = marginW + w * (i % oneRowNum) + (i / onePageNum) * _scrollView.width;
        y = marginH + h * ((i % onePageNum) / oneRowNum);
        UIButton *faceBtn =[[UIButton alloc] init];
        faceBtn.frame = CGRectMake(x, y, w, h);
        faceBtn.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        [_scrollView addSubview:faceBtn];
        if (i < 10) {
            NSString *str = [NSString stringWithFormat:@"f00%d",i];
            [faceBtn setImage:[UIImage imageNamed:str] forState:UIControlStateNormal];
        }else{
            NSString *str = [NSString stringWithFormat:@"f0%d",i];
            [faceBtn setImage:[UIImage imageNamed:str] forState:UIControlStateNormal];
        }
    
        faceBtn.tag = i;
        [faceBtn addTarget:self action:@selector(_faceBthClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
}


#pragma mark - 滑动视图代理
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGFloat offset = scrollView.contentOffset.x;
    
    _pageControl.currentPage = offset / scrollView.width;
    
}

#pragma mark - 点击
// 点击某个表情
- (void)_faceBthClick:(UIButton *)button {
    
    if ([self.delegate respondsToSelector:@selector(faceViewDidChooseFace:)]) {
        [self.delegate faceViewDidChooseFace:arrEmotion[button.tag]];
    }
}

//获取默认表情数组
- (NSArray *)defaultEmoticons {
    NSMutableArray *array = [NSMutableArray new];
//    for (int i=0x1F600; i<=0x1F64F; i++) {
//        if (i < 0x1F641 || i > 0x1F644) {
//            int sym = EMOJI_CODE_TO_SYMBOL(i);
//            NSString *emoT = [[NSString alloc] initWithBytes:&sym length:sizeof(sym) encoding:NSUTF8StringEncoding];
//            [array addObject:emoT];
//            NSData *data = [emoT dataUsingEncoding:NSUTF8StringEncoding];
//  }
//}
    for (int i = 0; i < 92; i++) {
        if (i < 10) {
            NSString *emoT = [[NSString alloc] initWithFormat:@"[:f00%d}",i ];
            [array addObject:emoT];

        }else{
            NSString *emoT = [[NSString alloc] initWithFormat:@"[:f0%d}",i ];
            [array addObject:emoT];
        }
    }
    return array;
}



#pragma mark - 全局方法
- (void)show {
    [UIView animateWithDuration:0.25 animations:^{
        self.bottom = SCREEN_HEIGHT - 64;
    }];
    [self sendNotification];

}
- (void)hide {
    [UIView animateWithDuration:0.25 animations:^{
        self.top = SCREEN_HEIGHT - 64;
    }];
    [self sendNotification];

}
- (void)sendNotification {
    NSDictionary *info = [NSDictionary dictionaryWithObject:NSStringFromCGRect(self.frame) forKey:FaceViewFrameEndInfo];
    // 发送通知
    [_NOTIFICATION_CENTER postNotificationName:FaceViewFrameChange object:nil userInfo:info];

}
@end
